//
//  YTKNetworkAgent+ContentType.m
//  YTK_TTest
//
//  Created by admin on 2017/8/17.
//  Copyright © 2017年 clOudbb. All rights reserved.
//

#import "YTKNetworkAgent+ContentType.h"
#import <objc/runtime.h>
#import <AFNetworking.h>
@implementation YTKNetworkAgent (ContentType)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method orignalMethod = class_getInstanceMethod([self class],@selector(init));
        Method swizzledMethod = class_getInstanceMethod([self class],@selector(ytk_init));

        BOOL didAdd = class_addMethod([self class],
                                      @selector(init),
                                      method_getImplementation(swizzledMethod),
                                      method_getTypeEncoding(swizzledMethod));
        if (didAdd) {
            class_replaceMethod([self class],
                                @selector(ytk_init),
                                method_getImplementation(orignalMethod),
                                method_getTypeEncoding(orignalMethod));
        }else{
            method_exchangeImplementations(orignalMethod, swizzledMethod);
        }
    });
}

- (instancetype)ytk_init
{
    [self ytk_init];

    Ivar m = class_getInstanceVariable([self class], "_manager");
    AFHTTPSessionManager *manager = object_getIvar(self, m);
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
                                                                              @"text/html",
                                                                              @"text/json",
                                                                              @"text/plain",
                                                                              @"text/javascript",
                                                                              @"text/xml",
                                                                              @"image/*"]];
    return self;
}

@end
